import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_23: 点击开始游戏，加载完成后进入游戏		 ')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001053108?url_cache_key=ab49d6eabe9823050826460ca132aea4')
class TestCaseCarpark23:
    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        # 桌面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()
        # 车乐汇
        self.carpark_index_page = CommonPage(self.driver).get_carpark_index_page()
        self.carpark_recommend_page = CommonPage(self.driver).get_carpark_recommend_page()
        # 成语接龙
        self.carpark_idioms_index_page = CommonPage(self.driver).get_carpark_idioms_page()
        self.carpark_idioms_start_page = CommonPage(self.driver).get_carpark_idioms_start_page()
        self.carpark_idioms_game_page = CommonPage(self.driver).get_carpark_idioms_game_page()
        self.carpark_idioms_quit_toast_page = CommonPage(self.driver).get_carpark_idioms_quit_page()
        self.carpark_idioms_settlement_page = CommonPage(self.driver).get_carpark_idioms_settlement_page()
        self.carpark_idioms_ranking_page = CommonPage(self.driver).get_carpark_idioms_ranking_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('carpark')
        # XiaoMaUtils.clear_data('personal')
        self.driver.quit()

    @allure.story('step1:点击应用页')
    @pytest.mark.run(order=231)
    def test_carpark_23_1_click_list(self):
        try:
            self.launcher_index_page.click_app_list()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step2: 点击列表中的车乐汇图标")
    @pytest.mark.run(order=232)
    def test_carpark_23_2_click_carpark(self):
        try:
            self.launcher_app_list_page.click_appname("车乐汇")
            time.sleep(5)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step3: 点击游戏tab进入游戏,点击成语接龙进入成语接龙")
    @pytest.mark.run(order=233)
    def test_carpark_23_3_is_display_carpark(self):
        try:
            self.carpark_recommend_page.is_display_carpark_recommend()
            self.carpark_recommend_page.click_tab_game()
            self.carpark_index_page.is_display_carpark_index()
            self.carpark_index_page.click_list_name(index=2)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)


    @allure.story("step4: 点击开始游戏,查看是否进入游戏中")
    @pytest.mark.run(order=234)
    def test_carpark_23_4_is_display_carpark(self):
        try:
            a = self.carpark_idioms_start_page.is_display_carpark_start()
            if a == True:
                self.carpark_idioms_start_page.click_start_game()
                self.carpark_idioms_game_page.is_display_carpark_game()
            else:
                self.carpark_idioms_index_page.click_download_immediately()
                time.sleep(40)
                self.carpark_idioms_start_page.is_display_carpark_start()
                self.carpark_idioms_start_page.click_start_game()
                self.carpark_idioms_game_page.is_display_carpark_game()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)


if __name__ == 'main__':
    # pass
    pytest.main("-s testcase_carpark_23.py")
